
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;


import messagepasser.DocInfo;
import messagepasser.DocListDAOSQL;
import messagepasser.LoginDAOSQL;
import messagepasser.UserInfo;
import messagepasser.UserInfoDAOSQL;

public class Server_Servant extends UnicastRemoteObject implements Server {
	
	List <Client> onlineUsers = new ArrayList <Client>();
		
	public Server_Servant() throws RemoteException {
	}	
	public UserInfo login (String user, String pwd) throws RemoteException {
		
		LoginDAOSQL logindao = new LoginDAOSQL("com.mysql.jdbc.Driver","jdbc:mysql:///tecbox","userprofile");
		String pwddb = logindao.getPwd(user);
		if(pwddb!=null){
			if (pwddb.equals(pwd))
			System.out.println("Login Successful");
			
		}
		else {
			System.out.println("Login Unsuccessful");
			return null;
		}
		int userid = logindao.getUserID(user);
		
		UserInfo userInformation = new UserInfo(userid, user, true, "127.0.0.1", 14355);
		for (Client c : onlineUsers) {
			c.newUserOnline(userInformation);
		}
		
		return userInformation; 			

	}
	
	public List<DocInfo> getDocuments(int userId) throws RemoteException {
	
		DocListDAOSQL docDAO = new DocListDAOSQL("com.mysql.jdbc.Driver","jdbc:mysql:///tecbox","document");
		return docDAO.getDocsInfo(userId);
		 			
	}
	
	public UserInfo[] getonlineUsers(int userId) throws RemoteException {
	// TODO Auto-generated method stub
		UserInfoDAOSQL userDAO = new UserInfoDAOSQL("com.mysql.jdbc.Driver","jdbc:mysql:///tecbox","user");
		return userDAO.getuserinfo(userId);
	}
	@Override
	public void register(Client c) throws RemoteException {
		this.onlineUsers.add(c);
	}
	
	
}
